(function($) {

	var dd_search = {
		parentElement : "",
		sendRequest : function(event) {
			dd_search.parentElement = event.data.parentElement;
			console.log("dd_sendRequest");
			$.ajax({
				dataType: "json",
				url: dasding.config.eventsListUrl,
				success: dd_search.renderData
			});
		},

		renderData : function(data) {
			console.log("renderData");
			if($('.dd_mapcontainer' ,dd_search.parentElement)) { dd_search.renderMap(data);} 
			dd_search.renderDetails(data);
		  dd_search.renderList(data);
		},

		renderMap : function(data) {
			console.log("renderMap");

			  map = new OpenLayers.Map("dd_mapcontainer");
        var mapnik = new OpenLayers.Layer.OSM();
        map.addLayer(mapnik);
        // map.setCenter(new OpenLayers.LonLat(52.4750231, 13.4251809) // Center of the map
        //   .transform(
        //     new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
        //     new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
        //   ), 12 // Zoom level
        // );

        var markers = new OpenLayers.Layer.Markers( "Markers" );
		    map.addLayer(markers);

		    var size = new OpenLayers.Size(21,25);
		    var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
		    var icon = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker.png', size, offset);

		    $.each(data, function(key, value) {
		    	console.log(key);
		 			var lonlat = new OpenLayers.LonLat( value.location.longitude,value.location.latitude )
			      .transform(
			        new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
			        map.getProjectionObject() // to Spherical Mercator Projection
			      );

		      var marker;

		    	if(key == 0)
			    	marker = new OpenLayers.Marker(lonlat ,icon);
			    else
			    	marker = new OpenLayers.Marker(lonlat,icon.clone());

			    console.log(value);
			    var markerDetails = "<strong>" + value.name + "</strong><br />" + value.description + "<br /><hr /><br />" + value.location.name + ": " + value.location.zip + " " + value.location.city;

					marker.events.register("click", marker, function() {
					  $("#dd_mapdetails").html(markerDetails);
					});

			    markers.addMarker(marker);

		    });





			  markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(52, 13), icon));
			  markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(51,13), icon.clone()));

        map.setCenter(new OpenLayers.LonLat(13.4251809, 52.4750231) // Center of the map
          .transform(
            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
            new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
          ), 10 // Zoom level
        );

		    console.log(markers);



	


		},

		renderDetails : function(data) {
			console.log("renderDetails");
		},

		renderList : function(data) {
			console.log("renderList");
		}


	};

  $.fn.dd_search = function() {

  	console.log($(this));
  	return $(this).each( function(){
  		console.log("hier");

  		var $this = jQuery(this);
  		$('.dd_filterbutton', $this).bind('click', {parentElement: $this}, dd_search.sendRequest);

  		// dd_search.sendRequest();
  	})
  	// $(this).each(function() {

  	// 	$('.dd_filterbutton', this).bind('click')
  	// 	console.log($(this));
  	// });

  }

})(jQuery);